package com.android.base.widget.loading;


import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.base.R;


public class VaryViewHelperController {

    private IVaryViewHelper helper;

    private ImageView loadingImageView;

    private AnimationDrawable animationDrawable;

    private AnimationDrawable mDrawableAnim;

    private ImageView mImageView;

    public VaryViewHelperController(View view) {

        this(new VaryViewHelper(view));
    }

    public VaryViewHelperController(IVaryViewHelper helper) {
        super();
        this.helper = helper;
    }

    public void showNetworkError(View.OnClickListener onClickListener) {
        View layout = helper.inflate(R.layout.view_loadding_error);
        Button againBtn = (Button) layout.findViewById(R.id.pager_error_loadingAgain);
        if (null != onClickListener) {
            againBtn.setOnClickListener(onClickListener);
        }
        helper.showLayout(layout);
    }

    public void showEmpty(String emptyMsg,int resoulce) {
        View layout = helper.inflate(R.layout.view_loadding_no_data);
        TextView textView = (TextView) layout.findViewById(R.id.tv_no_data);
        ImageView img = (ImageView) layout.findViewById(R.id.iv_no_data);

        img.setImageResource(resoulce);

        if (!TextUtils.isEmpty(emptyMsg)) {
            textView.setText(emptyMsg);
        }
        helper.showLayout(layout);
    }

    public void showLoadError(String emptyMsg,int resoulce,View.OnClickListener onClickListener) {

        View layout = helper.inflate(R.layout.view_loadding_error);
        Button againBtn = (Button) layout.findViewById(R.id.pager_error_loadingAgain);

        TextView textView = (TextView) layout.findViewById(R.id.network_error_info);
        ImageView imageView = (ImageView) layout.findViewById(R.id.network_error_img);

        if (null != onClickListener) {
            againBtn.setOnClickListener(onClickListener);
        }
        helper.showLayout(layout);


        imageView.setImageResource(resoulce);

        if (!TextUtils.isEmpty(emptyMsg)) {
            textView.setText(emptyMsg);
        }

        helper.showLayout(layout);
    }



    public void showLoading() {
        View layout = helper.inflate(R.layout.view_loading_loading);

        if(loadingImageView==null){
            loadingImageView = (ImageView) layout.findViewById(R.id.loading_img);
        }

        if(animationDrawable==null){
            animationDrawable = (AnimationDrawable) loadingImageView.getDrawable();
        }

        animationDrawable.start();


        helper.showLayout(layout);
    }

    public void restore() {

        if(animationDrawable!=null){
            animationDrawable.stop();
            animationDrawable = null;
        }

        helper.restoreView();
    }
}
